package n12.dhcntt.k3;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Activity_GiaiPhuongTrinhBachai extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity__giai_phuong_trinh_bachai);
	}
	
	public void giaiphuongtrinhbac2(View view){
		EditText soa = (EditText) findViewById(R.id.txtSo1);
		EditText sob = (EditText) findViewById(R.id.txtSo2);
		EditText soc = (EditText) findViewById(R.id.txtSo3);
		
		double a = Double.parseDouble(soa.getText().toString());
		double b = Double.parseDouble(sob.getText().toString());
		double c = Double.parseDouble(soc.getText().toString());
		
		TextView txt = (TextView) findViewById(R.id.lblResult);
		
		try {
			if(soa.getText().length() <= 0 || sob.getText().length() <= 0 || soc.getText().length() <= 0){
				txt.setText("Du lieu rong!");
			}else{
				if(a == 0){
					txt.setText("Day la phuong trinh bac nhat!");
					//Toast.makeText(this, "Test"+a, 2000).show();
					if(b==0){
						if(c == 0){
							txt.setText("Phuong trinh co vo so nghiem");
						}else{
							txt.setText("Phuong trinh vo nghiem");
						}
					}else{
						//truong hop a=0, b!=0, c!=0
						double kq = c/b;
						txt.setText("Phuong trinh co nghiem: "+kq);
					}
				}else{
					double denta = (b*b)-(4*a*c);
					if(denta < 0){
						txt.setText("Phuong trinh vo nghiem");
					}else if(denta == 0){
						double kq = -b/2*a;
						txt.setText("Phuong trinh co nghiem kep"+kq);
					}else{
						double denta_sqrt = Math.sqrt(denta);
						
						double x1 = (-b+denta_sqrt)/(2*a);
						double x2 = (-b-denta_sqrt)/(2*a);
						
						txt.setText("Phuong trinh co 2 nghiem phan biet: x1= "+x1+", x2 = "+x2);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}